package com.xyht.medicine_management_system.modules.payment.alipay.util;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.*;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.wechat.pay.java.core.notification.RequestParam;
import com.wechat.pay.java.service.payments.app.model.*;
import com.wechat.pay.java.service.payments.model.Transaction;
import com.wechat.pay.java.service.profitsharing.model.OrdersEntity;
import com.wechat.pay.java.service.profitsharing.model.QueryOrderRequest;
import com.wechat.pay.java.service.profitsharing.model.UnfreezeOrderRequest;
import com.wechat.pay.java.service.refund.model.AmountReq;
import com.wechat.pay.java.service.refund.model.CreateRequest;
import com.wechat.pay.java.service.refund.model.QueryByOutRefundNoRequest;
import com.wechat.pay.java.service.refund.model.Refund;
import com.xyht.medicine_management_system.common.util.TimeUtil;
import com.xyht.medicine_management_system.modules.payment.alipay.config.AlipayInitConfig;
import com.xyht.medicine_management_system.modules.payment.wxpay.config.WxPayConfig;
import com.xyht.medicine_management_system.modules.payment.wxpay.entity.CallbackPayResourceEntity;
import com.xyht.medicine_management_system.modules.payment.wxpay.entity.PrepayPaymentReq;
import com.xyht.medicine_management_system.modules.payment.wxpay.entity.RefundReq;
import com.xyht.medicine_management_system.modules.wx.entity.WxMpPropertiesEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

import java.util.List;

import static com.xyht.medicine_management_system.common.util.ParamCheckUtil.isNullOrEmpty;
import static com.xyht.medicine_management_system.modules.payment.constant.CommonConstant.*;

@Component
public class AliPayUtil {
    /**
     * @description 订单预支付 生成预支付码给前端
     * @create 2023/12/21
     * @author dxn
     */
    public String prepayPayment(PrepayPaymentReq prepayPaymentReq) {
        if (isNullOrEmpty(prepayPaymentReq.getOutTradeNo(), prepayPaymentReq.getDescription(),
                prepayPaymentReq.getTimeExpire(), prepayPaymentReq.getTotal())) {
            return null;
        }
        AlipayClient alipayClient;
        AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
        AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
        AlipayTradeAppPayResponse response;
        try {
            alipayClient = new DefaultAlipayClient(AlipayInitConfig.alipayConfig);
            model.setOutTradeNo(prepayPaymentReq.getOutTradeNo());
            model.setTotalAmount(ToolUtil.numDiv(prepayPaymentReq.getTotal(), 100, 2));
            model.setSubject(prepayPaymentReq.getDescription());
            model.setTimeExpire(TimeUtil.DateFormatSting(prepayPaymentReq.getTimeExpire()));

            RoyaltyInfo royaltyInfo = new RoyaltyInfo();
            royaltyInfo.setRoyaltyType("ROYALTY");
            model.setRoyaltyInfo(royaltyInfo);

            request.setBizModel(model);
            request.setNotifyUrl(ALI_NOTIFY_PAY_CALLBACK);
            response = alipayClient.sdkExecute(request);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if (!response.isSuccess()) {
            return null;
        }
        return response.getBody();
    }


    /**
     * @param order_id 订单号
     * @description 订单状态查询
     * @create 2023/12/24
     * @author dxn
     */
    public AlipayTradeQueryResponse queryOrderByOutTradeNo(String order_id) {
        AlipayTradeQueryResponse response;
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayInitConfig.alipayConfig);
            AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
            AlipayTradeQueryModel model = new AlipayTradeQueryModel();
            model.setOutTradeNo(order_id);
            request.setBizModel(model);
            response = alipayClient.execute(request);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if (!response.isSuccess()) {
            return null;
        }
        return response;
    }

    /**
     * @description 用户退款
     * @create 2023/12/24
     * @author dxn
     */
    public AlipayTradeRefundResponse createRefund(RefundReq refundReq) {
        AlipayTradeRefundResponse response;
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayInitConfig.alipayConfig);
            AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
            AlipayTradeRefundModel model = new AlipayTradeRefundModel();
            model.setRefundAmount(ToolUtil.numDiv(refundReq.getRefund(), 100, 2));
            model.setTradeNo(refundReq.getTransaction_id());
            model.setOutTradeNo(refundReq.getOut_trade_no());
            model.setOutRequestNo(refundReq.getOut_refund_no());
            model.setRefundCurrency("CNY");

            request.setBizModel(model);
            request.setNotifyUrl(ALI_NOTIFY_PAY_CALLBACK);
            response = alipayClient.execute(request);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if (!response.isSuccess()) {
            return null;
        }
        return response;
    }

    /**
     * @param refund_id 退款单号
     * @description 用户退款查询
     * @create 2023/12/25
     * @author dxn
     */
    public AlipayTradeFastpayRefundQueryResponse queryByOutRefundNo(String refund_id) {
        AlipayTradeFastpayRefundQueryResponse response;
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayInitConfig.alipayConfig);
            AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();
            AlipayTradeFastpayRefundQueryModel model = new AlipayTradeFastpayRefundQueryModel();
            model.setOutRequestNo(refund_id);
            request.setBizModel(model);
            response = alipayClient.execute(request);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if (!response.isSuccess()) {
            return null;
        }
        return response;
    }

    /**
     * @param order_id       订单号
     * @param transaction_id 支付宝订单号
     * @description 分账完结解除冻结
     * @create 2023/12/25
     * @author dxn
     */
    public AlipayTradeOrderSettleResponse profitSharingUnfreezeOrder(String order_id, String transaction_id) {
        AlipayTradeOrderSettleResponse response;
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayInitConfig.alipayConfig);

            // 构造请求参数以调用接口
            AlipayTradeOrderSettleRequest request = new AlipayTradeOrderSettleRequest();
            AlipayTradeOrderSettleModel model = new AlipayTradeOrderSettleModel();

            // 设置结算请求流水号
            model.setOutRequestNo(order_id);

            // 设置支付宝订单号
            model.setTradeNo(transaction_id);

            // 设置分账明细信息
//            List<OpenApiRoyaltyDetailInfoPojo> royaltyParameters = new ArrayList<OpenApiRoyaltyDetailInfoPojo>();
//            OpenApiRoyaltyDetailInfoPojo royaltyParameters0 = new OpenApiRoyaltyDetailInfoPojo();
//            royaltyParameters0.setRoyaltyType("transfer");
//            royaltyParameters0.setTransInType("userId");
//            royaltyParameters0.setTransIn("2088101126708402");
//            royaltyParameters0.setAmount("0.1");
//            royaltyParameters0.setDesc("分账给2088101126708402");
//            royaltyParameters.add(royaltyParameters0);
//            model.setRoyaltyParameters(royaltyParameters);

            request.setBizModel(model);
            request.setNotifyUrl(ALI_NOTIFY_PAY_PROFIT_SHARING_CALLBACK);
            response = alipayClient.execute(request);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if (!response.isSuccess()) {
            return null;
        }
        return response;
    }

    /**
     * @param order_id       订单号
     * @param transaction_id 微信订单号
     * @description 分账结果查询
     * @create 2023/12/25
     * @author dxn
     */
    public AlipayTradeOrderSettleQueryResponse profitSharingQueryOrder(String order_id, String transaction_id) {
        AlipayTradeOrderSettleQueryResponse response;
        try {
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayInitConfig.alipayConfig);

            // 构造请求参数以调用接口
            AlipayTradeOrderSettleQueryRequest request = new AlipayTradeOrderSettleQueryRequest();
            AlipayTradeOrderSettleQueryModel model = new AlipayTradeOrderSettleQueryModel();
            // 设置结算请求流水号
            model.setOutRequestNo(order_id);
            // 设置支付宝订单号
            model.setTradeNo(transaction_id);
            request.setBizModel(model);
            response = alipayClient.execute(request);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        if (!response.isSuccess()) {
            return null;
        }
        return response;
    }


}
